package com.eshop.gen.rest;

import com.eshop.exception.BadRequestException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Iterator;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.eshop.gen.domain.ColumnConfig;
import java.util.List;
import com.eshop.utils.PageUtil;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Value;
import com.eshop.gen.service.GenConfigService;
import com.eshop.gen.service.GeneratorService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({ "/api/generator" })
@Api(tags = { "系统：代码生成管理" })
public class GeneratorController
{
    private final GeneratorService generatorService;
    private final GenConfigService genConfigService;
    @Value("${generator.enabled}")
    private Boolean generatorEnabled;
    
    public GeneratorController(final GeneratorService generatorService, final GenConfigService genConfigService) {
        this.generatorService = generatorService;
        this.genConfigService = genConfigService;
    }
    
    @ApiOperation("查询数据库数据")
    @GetMapping({ "/tables/all" })
    public ResponseEntity<Object> getTables() {
        return (ResponseEntity<Object>)new ResponseEntity(this.generatorService.getTables(), HttpStatus.OK);
    }
    
    @ApiOperation("查询数据库数据")
    @GetMapping({ "/tables" })
    public ResponseEntity<Object> getTables(@RequestParam(defaultValue = "") final String name, @RequestParam(defaultValue = "0") final Integer page, @RequestParam(defaultValue = "10") final Integer size) {
        return (ResponseEntity<Object>)new ResponseEntity(this.generatorService.getTables(name, page, size), HttpStatus.OK);
    }
    
    @ApiOperation("查询字段数据")
    @GetMapping({ "/columns" })
    public ResponseEntity<Object> getTables(@RequestParam final String tableName) {
        final List<ColumnConfig> columnInfos = this.generatorService.getColumns(tableName);
        return (ResponseEntity<Object>)new ResponseEntity((Object)PageUtil.toPage((Object)columnInfos, (Object)columnInfos.size()), HttpStatus.OK);
    }
    
    @ApiOperation("保存字段数据")
    @PutMapping
    public ResponseEntity<HttpStatus> save(@RequestBody final List<ColumnConfig> columnInfos) {
        this.generatorService.save(columnInfos);
        return (ResponseEntity<HttpStatus>)new ResponseEntity(HttpStatus.OK);
    }
    
    @ApiOperation("同步字段数据")
    @PostMapping({ "sync" })
    public ResponseEntity<HttpStatus> sync(@RequestBody final List<String> tables) {
        for (final String table : tables) {
            this.generatorService.sync(this.generatorService.getColumns(table), this.generatorService.query(table));
        }
        return (ResponseEntity<HttpStatus>)new ResponseEntity(HttpStatus.OK);
    }
    
    @ApiOperation("生成代码")
    @PostMapping({ "/{tableName}/{type}" })
    public ResponseEntity<Object> generator(@PathVariable final String tableName, @PathVariable final Integer type, final HttpServletRequest request, final HttpServletResponse response) {
        if (!this.generatorEnabled && type == 0) {
            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
        }
        switch (type) {
            case 0: {
                this.generatorService.generator(this.genConfigService.find(tableName), this.generatorService.getColumns(tableName));
                break;
            }
            case 1: {
                return this.generatorService.preview(this.genConfigService.find(tableName), this.generatorService.getColumns(tableName));
            }
            case 2: {
                this.generatorService.download(this.genConfigService.find(tableName), this.generatorService.getColumns(tableName), request, response);
                break;
            }
            default: {
                throw new BadRequestException("没有这个选项");
            }
        }
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
}
